package com.huluobo.high3demo.setting;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.SettingBean;
import com.huluobo.mvvm.viewmodel.BaseViewModel;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LC on 2023/10/7.
 */
public class SettingViewModel extends BaseViewModel<SettingRepo> {
    MutableLiveData<SettingBean> updateIconSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateIconFailed = new MutableLiveData<>();
    MutableLiveData<SettingBean> updateAddressSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateAddressFailed = new MutableLiveData<>();
    MutableLiveData<SettingBean> updateNicknameSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateNicknameFailed = new MutableLiveData<>();
    MutableLiveData<SettingBean> updateBrithSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateBrithFailed = new MutableLiveData<>();
    MutableLiveData<SettingBean> updateSexSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateSexFailed = new MutableLiveData<>();

    public void updateUserIcon(String filePath) {
        //1.创建文件对象
        File file = new File(filePath);
        //2.封装body
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        //3.封装part,file是接口文档中文件的参数, 第一个参数是接口中的参数性,第二个参数是文件名,第三个参数是requestBody
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        //4.请求接口
        repo.updateUserIcon(part, updateIconSuccess, updateIconFailed);
    }

    public void updateUserNickname(String nickName) {
        repo.updateUserNickname(nickName, updateNicknameSuccess, updateNicknameFailed);
    }

    public void updateUserAddress(String address) {
        repo.updateAddress(address, updateAddressSuccess, updateAddressFailed);
    }

    public void updateUserBrith(String brith) {
        repo.updateBrith(brith, updateBrithSuccess, updateBrithFailed);
    }

    public void updateUserSex(String sex) {
        repo.updateSex(sex, updateSexSuccess, updateSexFailed);
    }

    public SettingViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    protected SettingRepo createRepo() {
        return new SettingRepo();
    }
}
